package wang.hu.bos.service.system.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wang.hu.bos.dao.system.MenuDao;
import wang.hu.bos.dao.system.PermissionDao;
import wang.hu.bos.dao.system.RoleDao;
import wang.hu.bos.domain.system.Menu;
import wang.hu.bos.domain.system.Permission;
import wang.hu.bos.domain.system.Role;
import wang.hu.bos.service.system.RoleService;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;
	@Autowired
	private PermissionDao permissionDao;
	@Autowired
	private MenuDao menuDao;

	@Override
	public List<Role> findAll() {

		return roleDao.findAll();
	}

	@Override
	public void save(Role role, String[] permissionIds, String menuIds) {
		if (permissionIds != null) {
			for (String id : permissionIds) {
				Permission permission = permissionDao.findOne(Integer
						.parseInt(id));
				role.getPermissions().add(permission);
			}
		}
		if (StringUtils.isNoneBlank(menuIds)) {
			for (String mId : menuIds.split(",")) {
				Menu menu = menuDao.findOne(Integer.parseInt(mId));
				role.getMenus().add(menu);
			}
		}
		roleDao.save(role);

	}

}
